<template>
  <!-- 数据层级大于二级, 判断一级有数据才渲染 => v-if="detail.categories" -->
  <div class="xtx-goods-page" v-if="detail.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <!-- 默认插槽 -->
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 一级分类 -->
        <XtxBreadItem to="/">{{ detail.categories[1].name }}</XtxBreadItem>
        <!-- 二级分类 -->
        <XtxBreadItem to="/">{{ detail.categories[0].name }}</XtxBreadItem>
        <!-- 二级分类下的当前商品 -->
        <XtxBreadItem to="/">{{ detail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <LeftImgs :list="detail.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName />
          <!-- Sku选择 => 商品属性选择 -->
          <GoodsSku :goods="detail" @change="selSku" />
          <!-- 数量选择 -->
          <XtxNumbox v-model="buyNum" :max="detail.inventory" />
          <!-- v-modal完整写法 -->
          <!-- <XtxNumbox
            :modelValue="buyNum"
            @update:modelValue="buyNum = $event"
            :max="detail.inventory"
          /> -->
          <!-- 加入购物车按钮 -->
          <XtxButton size="large" type="primary">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <img
              v-imglazy="url"
              v-for="url in detail.details.pictures"
              :key="url"
            />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, provide } from 'vue'
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
// 左侧图片列表
import LeftImgs from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'

export default {
  name: 'XtxGoodsPage',
  components: {
    LeftImgs,
    GoodsSales,
    GoodsName
  },
  setup () {
    const detail = ref([])
    // 购买数量
    const buyNum = ref(1)
    // 在setup第一层注入共享数据
    provide('detail', detail)
    // 获取路由参数: 1. query(?参数) 2. params(:参数)
    const route = useRoute()
    console.log('vue3获取路由参数: ', route)
    const getDetail = async () => {
      const { result } = await findGoods(route.params.id)
      console.log('商品详情: ', result)
      detail.value = result
      // 注入数据 (不允许嵌套)
      // provide('detail', detail)
    }
    getDetail()

    // sku选择回调函数
    const selSku = (skuInfo) => {
      // 判断是否有效的sku信息 (不为空)
      console.log('选中的sku信息: ', skuInfo)
      if (skuInfo.skuId) {
        // 显示选择之后的价格并更新库存
        detail.value.price = skuInfo.price
        detail.value.oldPrice = skuInfo.oldPrice
        detail.value.inventory = skuInfo.inventory
      }
    }
    return { detail, selSku, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
